import axios from 'axios';

// DeepSeek API 地址
const deepSeekApiUrl = 'https://api.deepseek.com/chat/completions';
// 请替换为你自己的 DeepSeek API Token
const apiToken = 'sk-16803c60296a45749de31dfa8b3cf462';

// 定义发送请求的函数，接受消息历史记录作为参数
export const sendDeepSeekRequest = async (chatHistory: { isUser: boolean, content: string }[]) => {
    const requestData = {
        messages: [
            {
                content: "You are a helpful assistant",
                role: "system"
            },
            ...chatHistory.map(msg => ({
                role: msg.isUser ? 'user' : 'assistant',
                content: msg.content
            }))
        ],
        model: "deepseek-chat",
        frequency_penalty: 0,
        max_tokens: 2048,
        presence_penalty: 0,
        response_format: {
            type: "text"
        },
        stop: null,
        stream: false,
        stream_options: null,
        temperature: 1,
        top_p: 1,
        tools: null,
        tool_choice: "none",
        logprobs: false,
        top_logprobs: null
    };

    try {
        const response = await axios({
            method: 'post',
            url: deepSeekApiUrl,
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': `Bearer ${apiToken}`
            },
            data: requestData
        });

        return response.data.choices[0].message.content;
    } catch (error) {
        console.error('请求 DeepSeek API 出错:', error);
        return '抱歉，出现错误，无法获取回复';
    }
};